/**
 * 
 */
package com.googlecode.bestpaper.output.pdf.link;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.StringUtils;
import org.umlgraph.doclet.UmlGraph;

import com.googlecode.bestpaper.syntax.link.BPLink;
import com.googlecode.bestpaper.utils.BestpaperUtils;
import com.lowagie.text.Image;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Section;

/**
 * @author xalinx at gmail dot com
 * @date Jan 26, 2010
 */
public class PDFLink extends BPLink<Object> {

	/**
	 * 
	 */
	public PDFLink() {
		super();
	}

	/**
	 * @param link
	 */
	public PDFLink(String link) {
		super(link);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.googlecode.bestpaper.syntax.BPMarkup#build(java.lang.Object,
	 * int)
	 */
	@Override
	public void build(Object writer, int fontStyle) throws Exception {
		String link = this.getLink();
		link = StringUtils.trim(link);
		if (StringUtils.isEmpty(link))
			return;
		String[] cmds = StringUtils.split(link, ' ');
		if (cmds.length >= 2) {
			String cmd1 = cmds[0];
			String cmd2 = cmds[1];
			if ("uml:class".equals(cmd1) && cmd2.endsWith(".java")) {
				String dotFile = cmd2 + ".dot";
				String dotOutput = cmd2 + ".png";
				runDot(cmd2, dotFile, dotOutput);
				link = dotOutput;
			}
		}

		if (link.endsWith("png") || link.endsWith("jpg") || link.endsWith("jpeg") || link.endsWith("gif")) {
			if (!com.googlecode.bestpaper.utils.FileUtils.isAbsolutePath(link) && !StringUtils.isEmpty(BestpaperUtils.getInputFileDirPath())) {
				link = BestpaperUtils.getInputFileDirPath() + File.separatorChar + this.getLink();
			}
			Image image = Image.getInstance(link);
			image.setAlignment(Image.LEFT);
			float width = image.getWidth();
			float height = image.getWidth();
			float[] docSize = BestpaperUtils.getDocumentSize();
			float docWidth = docSize[0];
			float docHeight = docSize[1];
			float scaleRate = 1;
			if (width > docWidth) {
				scaleRate = docWidth / width;
			}
			if (height > docHeight) {
				float newscaleRate = docHeight / height;
				if (newscaleRate < scaleRate) {
					scaleRate = newscaleRate;
				}
			}
			image.scalePercent(scaleRate * 100);
			if (writer instanceof Section) {
				Section section = (Section) writer;
				section.add(image);
			} else if (writer instanceof Paragraph) {
				Paragraph para = (Paragraph) writer;
				para.add(image);
			}
		}

	}

	private void runDot(String javaFile, String dotFile, String dotOutput) throws IOException, InterruptedException {
		org.umlgraph.doclet.UmlGraph.main(new String[] { "-package", "-output", dotFile, javaFile });
		Process process = Runtime.getRuntime().exec(new String[] { "/usr/local/graphviz/bin/dot", "-Tpng", dotFile, "-o", dotOutput });
		int result = process.waitFor();
		if (result != 0)
			System.out.println("Errors running Graphviz on " + dotFile);
		process.destroy();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.googlecode.bestpaper.syntax.BPMarkup#getTitle()
	 */
	@Override
	public String getTitle() {
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.googlecode.bestpaper.syntax.BPMarkup#getTitleFontSize()
	 */
	@Override
	public int getTitleFontSize() {
		return 0;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.googlecode.bestpaper.syntax.BPMarkup#setTitleFontSize(int)
	 */
	@Override
	public void setTitleFontSize(int titleFontSize) {
		// TODO Auto-generated method stub

	}

}
